<?php
namespace Hr\OnlineBundle\Controller;

use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Doctrine\ORM\Tools\Pagination\Paginator;
use Doctrine\ORM\QueryBuilder;


class GenericController extends Controller
{
    public function init($locale=null)
    {
        // i18n - switch and set language
		$request = $this->getRequest(); 
		$locale = $locale ? $locale : $request->query->get('_locale');
		$session = $this->get('session'); // $this->getRequest()->getSession();
		if ($locale) {
			$locale = substr($locale, 0, 2);	
			$session = new Session();
			try {
				$session->start();
			} catch (\RuntimeException $e) {
				// already started
			}
			$session->set('_locale', $locale);
		} else {
			$locale = $session->get('_locale');
		}
		$request->setLocale($locale);
    }


		
	protected function getDoctrinePaginator(QueryBuilder $qb, $limit=10)
	{
		$page = $this->getRequest()->query->get("page", 1) ;

		$page = $page>0 ? $page : 1;

		$limit = $limit>0 ? $limit : $this->container->getParameter('hr_online.max_positions_per_page', 10);

		$qb ->setFirstResult( ( $page-1 ) * $limit)->setMaxResults($limit) ;
		
		return new Paginator($qb);
	}

}
